//
//  CustomImagePicker.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 25/02/12.
//  Copyright 2012 Mehfile Zainab. All rights reserved.
//

#import "CustomImagePicker.h"
#import "CustomImageViewer.h"
//#import "CustomImagePickerAppDelegate.h"
#import "UIImageExtras.h"

@implementation CustomImagePicker
@synthesize images = _images;
@synthesize thumbs = _thumbs;
//@synthesize selectedImage = _selectedImage;

@synthesize imageViewer = _imageViewer;

- (id) init {
	if ((self = [super init])) {
		_images =  [[NSMutableArray alloc] init];
		_thumbs =  [[NSMutableArray alloc] init];
	}
	return self;
}

- (void)addImage:(UIImage *)image {
    //NSLog(@"Initial size of images array : %d",[_images count]);
    //UIImage *current = [[image copy] autorelease];
    //[_images addObject:current];
    //if (image) { NSLog(@"Object is not nil"); }
    //else { NSLog(@"WARN : Object is NIL"); }
	[_images addObject:image];
    //NSLog(@"Initial size of thumbs array : %d",[_thumbs count]);
	[_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(64, 64)]];
    NSLog(@"Size of images array : %d",[_images count]);
}

- (void)viewDidLoad {
	
	// Create view
	UIScrollView *view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	int row = 0;
	int column = 0;
	for(int i = 0; i < _thumbs.count; ++i) {
		
		UIImage *thumb = [_thumbs objectAtIndex:i];
		UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
		button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
		[button setImage:thumb forState:UIControlStateNormal];
		[button addTarget:self 
				   action:@selector(buttonClicked:) 
		 forControlEvents:UIControlEventTouchUpInside];
		button.tag = i; 
		[view addSubview:button];

		if (column == 2) {
			column = 0;
			row++;
		} else {
			column++;
		}
		
	}
	
	view.backgroundColor = [UIColor clearColor];
	[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
	
	self.view = view;
	[view release];
	
	// Create cancel button
	/*
	UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] 
									 initWithTitle:@"Cancel" 
									 style:UIBarButtonItemStylePlain 
									 target:self 
									 action:@selector(cancel:)];
	self.navigationItem.leftBarButtonItem = cancelButton;
	[cancelButton release];
	*/
	 
	[super viewDidLoad];
}

-(IBAction)GoBack:(id)sender
{
	//[self dismissModalViewControllerAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)buttonClicked:(id)sender {
	
	UIButton *button = (UIButton *)sender;
	//self.selectedImage = [_images objectAtIndex:button.tag];
	//CustomImagePickerAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	//[delegate.navController popViewControllerAnimated:YES];
	
    NSLog(@"Button %d clicked",button.tag);
    
	_imageViewer = [[CustomImageViewer alloc] init];
	//_imageViewer.selectedImage = [_images objectAtIndex:button.tag];
	_imageViewer.currentPage = button.tag;
	_imageViewer._images = _images;
	//_imageViewer.title = @"Mehfile Zainab";
    _imageViewer.title = [NSString stringWithFormat:@"%d / %d",button.tag+1,[_images count]];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                      initWithTitle:@"Accueil"
                                      style:UIBarButtonItemStyleBordered
                                      target:self
                                      action:@selector(GoBack:)];
    _imageViewer.navigationItem.rightBarButtonItem = barButtonItem;
    
	[self.navigationController pushViewController:_imageViewer animated:YES];
	
}


/*
- (IBAction)cancel:(id)sender {
	self.selectedImage = nil;
	CustomImagePickerAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.navController popViewControllerAnimated:YES];
}
*/

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
	//self.images = nil;
	//self.thumbs = nil;
	//self.selectedImage = nil;
	[super dealloc];
}

@end

